﻿
Imports System.IO
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class products
    Inherits cacheControlProcessor

    Public Const _pathidentity As String = "/products"
    Public Const defaultImage As String = defaultLinkImage
    Public Const countquery As String = "c"
    Private WithEvents prodconfig As configMonitor = Nothing

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial()
        Dim file As String = Nothing
        file = config.parameter(configNaming.products.name, configNaming.products.productfile _
                                , configNaming.defaultValue.products.productfile)
        prodconfig = New configMonitor()
        assert(prodconfig.open(file), "cannot open productfile " + file)
    End Sub

    Protected Overrides Function retiredticks() As Long
        'manually retire according to configuration change
        Return -1
    End Function

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Overrides Function pathidentitylen() As Long
        Return strlen(pathidentity())
    End Function

    Private Sub prodconfig_ConfigChanged() Handles prodconfig.ConfigChanged
        cache.clear()
    End Sub

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        Dim links As vector(Of String) = Nothing
        Dim titles As vector(Of String) = Nothing
        Dim images As vector(Of String) = Nothing
        Dim sources As vector(Of String) = Nothing
        If prodconfig.readlist(configNaming.products.name, configNaming.products.link, links) _
            AndAlso prodconfig.readlist(configNaming.products.name, configNaming.products.title, titles) _
            AndAlso prodconfig.readlist(configNaming.products.name, configNaming.products.image, images) _
            AndAlso prodconfig.readlist(configNaming.products.name, configNaming.products.source, sources) Then
            Dim buff As MemoryStream = Nothing
            buff = context.buff()

            Dim page As xhtmlpage = Nothing
            page = New xhtmlpage(buff)
            page.title().text = applicationSign() + " - products"
            page.body().id = "id_prod"

            Dim csslink As csslink = Nothing
            csslink = New csslink(buff)
            csslink.href = csslinkhref
            page.head().addchild(csslink)

            Dim label As label = Nothing
            label = New label(buff)
            label.id = "id_label"
            label.text = "all open source applications from me"
            page.body().addchild(label)

            Dim prodcountStr As String = Nothing
            prodcountStr = context.url.query(countquery)
            Dim prodcount As Int64
            If Not isemptystring(prodcountStr) Then
                If Not convertor.toint64(prodcountStr, prodcount) Then
                    raiseError("cannot convert count " + prodcountStr + " in url to int64, use default value." _
                               , errorHandle.errorType.user)
                    prodcount = titles.size()
                End If
            Else
                prodcount = titles.size()
            End If

            Dim rails As rails = Nothing
            rails = New rails(buff)
            rails.increaseRails(2)
            page.body().addchild(rails)
            rails(0).id = "id_left"
            rails(1).id = "id_right"

            Dim linkset As linkset = Nothing
            linkset = New linkset(buff)
            linkset.setdata(links, titles, images, defaultImage, , prodcount)
            rails.addchild(linkset, 0)

            Dim a As a = Nothing
            For i = 0 To min(sources.size(), prodcount) - 1
                a = New a(buff)
                a.href = sources(i)
                a.text = "source - " + sources(i)
                rails.addchild(a, 1)
            Next

            If prodcount < titles.size() Then
                a = New a(buff)
                a.href = strmid(_pathidentity, strlen(network.pathSeperator))
                a.text = "see all applications"
                a.target = "_blank"
                page.body().addchild(a)
            End If

            control = page
            Return True
        Else
            raiseError("cannot read link list from linkconfig, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function
End Class
